package com.buka.goods.controller;


import com.buka.entity.Result;
import com.buka.entity.StatusCode;
import com.buka.goods.service.ClassifyService;
import com.buka.server.pojo.Classify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/classify")
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    @RequestMapping("/findAllFirst")
    public Result<List<Classify>> findAllFirst() {
        List<Classify> allFirst = classifyService.findAllFirst();
        return new Result<>(true, StatusCode.OK, "一级列表查询成功", allFirst);
    }

    @RequestMapping("/findAllByFirstId")
    public Result<List<Classify>> findAllByFirstId(@RequestParam Long id) {
        List<Classify> allFirst = classifyService.findAllByFirstId(id);
        return new Result<>(true, StatusCode.OK, "层次列表查询成功", allFirst);
    }

    @PostMapping("/addFirst")
    public Result addFirst(@RequestBody Classify classify) {
        int a = classifyService.AddFirstGoods(classify);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "增加失败");
        } else return new Result<>(true, StatusCode.OK, "增加主分类成功");
    }

    @PostMapping("/addNext")
    public Result addNext(@RequestBody Classify classify) {
        int a = classifyService.AddNextGoods(classify);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "增加失败");
        } else return new Result<>(true, StatusCode.OK, "增加子分类成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Classify classify) {
        int a = classifyService.updateGoods(classify);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "修改失败");
        } else return new Result<>(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {

        int a = classifyService.deleteGoods(id);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "失败");
        } else return new Result<>(true, StatusCode.OK, "成功");
    }


    @RequestMapping("/upload")
    public Result UpLoad(@RequestParam String brandName, @RequestParam(name = "file") MultipartFile file) {
        int i = classifyService.addAndFile(brandName, file);
        if(i!=1){
            return new Result<>(false, StatusCode.ERROR, "失败");
        }else return new Result<>(true, StatusCode.OK, "成功");

    }
}
